package org.zwy.gradetrack.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zwy.gradetrack.entity.Role;
import org.zwy.gradetrack.entity.UserRole;
import org.zwy.gradetrack.mapper.RoleMapper;
import org.zwy.gradetrack.mapper.UserRoleMapper;
import org.zwy.gradetrack.service.RoleValidationService;

import java.util.List;

/**
 * 角色验证服务实现类
 */
@Service
public class RoleValidationServiceImpl implements RoleValidationService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public boolean validateRoleExists(Long roleId) {
        return roleMapper.selectById(roleId) != null;
    }

    @Override
    public boolean validateRolesExist(List<Long> roleIds) {
        return roleMapper.selectBatchIds(roleIds).size() == roleIds.size();
    }

    @Override
    public Role getRole(Long roleId) {
        return roleMapper.selectById(roleId);
    }

    @Override
    public List<Role> getRoles(List<Long> roleIds) {
        return roleMapper.selectBatchIds(roleIds);
    }

    @Override
    public boolean hasRole(Long userId, Long roleId) {
        LambdaQueryWrapper<UserRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserRole::getUserId, userId)
                .eq(UserRole::getRoleId, roleId);
        return userRoleMapper.selectCount(wrapper) > 0;
    }

    @Override
    public List<UserRole> getUserRoles(Long userId) {
        LambdaQueryWrapper<UserRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserRole::getUserId, userId)
                .orderByDesc(UserRole::getCreateTime);
        return userRoleMapper.selectList(wrapper);
    }
} 